package com.gitee.feizns.explore.data_structure.tree.bs.mid;

/**
 * 面试题26. 树的子结构
 * @author feizns
 * @since 2020/3/20
 */
public class IsSubStructure {

    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if ( A != null && B != null ) {
            if ( contains(A, B) )
                return true;
            return isSubStructure(A.left, B) || isSubStructure(A.right, B);
        }
        return false;
    }

    private boolean contains(TreeNode A, TreeNode B) {
        if ( B != null ) {
            if ( A != null && B.val == A.val )
                return contains(A.left, B.left) && contains(A.right, B.right);
            return false;
        }
        return true;
    }

}
